﻿Partial Public Class ShowPictureResultPropertiesControl
    Private m_AvailableImages As List(Of MediaFile)

    Public Sub New(ByVal p_AvailableImages As List(Of MediaFile))
        InitializeComponent()
        AvailableImages = p_AvailableImages
        ComboImages.ItemsSource = m_AvailableImages

    End Sub



    Public Property AvailableImages() As List(Of MediaFile)
        Get
            Return m_AvailableImages
        End Get
        Set(ByVal value As List(Of MediaFile))
            m_AvailableImages = value
        End Set
    End Property


    Private Sub ComboImages_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)

        '  NotifyPropertyChanged("selectedImagePath")

        Dim l_imgs As New ImageSourceConverter
        Dim l_resultproperties = CType(Me.DataContext, ShowPictureResultProperties)

        Dim l_ImageSource = CType(l_imgs.ConvertFromString(l_resultproperties.Picture.Path), ImageSource)
        ImageBrush1.ImageSource = l_ImageSource
        '    ImageBrush1.ImageSource.SetValue(Image.SourceProperty, l_imgs.ConvertFromString(selectedImagePath))



    End Sub

    Private Sub ButtonLocation_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Try
            Dim l_button = CType(sender, Button)
            Dim l_Parent = getTopWindowGameEditor()

            Dim l_top = Math.Round(Canvas.GetTop(l_Parent.RubberBandProperty), 0)
            Dim l_bottom = Math.Round(l_top + l_Parent.RubberBandProperty.Height, 0)
            Dim l_left = Math.Round(Canvas.GetLeft(l_Parent.RubberBandProperty), 0)
            Dim l_right = Math.Round(l_left + l_Parent.RubberBandProperty.Width, 0)

            If l_top < 0 OrElse l_bottom < 0 OrElse l_right < 0 OrElse l_left < 0 OrElse l_top = l_bottom OrElse l_left = l_right Then
                MsgBox("Please draw the rectangle again and click here again. rectangle boundaries are out of range")
                Return
            End If

            Dim l_rec As New EventRectangle(New Point(l_left, l_top), New Point(l_right, l_bottom))
            Dim l_result = CType(Me.DataContext, ShowPictureResultProperties)
            l_result.Location = l_rec
            l_button.Content = l_rec.ToString
        Catch ex As Exception

        End Try
       
    End Sub


    Private Function getTopWindowGameEditor() As WindowGameEditor
        Dim l_Object As FrameworkElement = Me
        While l_Object IsNot Nothing
            If (TypeOf l_Object Is Window) Then
                Exit While

            End If
            If l_Object.Parent Is Nothing OrElse Not TypeOf l_Object.Parent Is FrameworkElement Then
                Exit While
            End If

            l_Object = CType(l_Object.Parent, FrameworkElement)

        End While
        If l_Object IsNot Nothing AndAlso TypeOf l_Object Is WindowGameEditor Then
            Return CType(l_Object, Window)
        Else
            Return Nothing
        End If

    End Function

   
    
    
End Class
